#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#

from common.common import exter_attack, output_result_file
from common.common_models import ActionResult, SubJobDetails
from common.const import ExecuteResultEnum, SubJobStatusEnum
from tpops.gaussdb.common.const import EnvName
from tpops.gaussdb.common.safe_get_information import ResourceParam
from tpops.gaussdb.handle.backup.exec_backup import BackUp
from tpops.gaussdb.handle.backup.gaussdb_backup_service import GaussDBBackupService
from tpops.gaussdb.common.log import log


class BackupAbility:

    def __init__(self):
        pass

    @staticmethod
    @exter_attack
    def backup(req_id, job_id, sub_id, json):
        log.info(
            f'start backup, req_id: {req_id}, job_id: {job_id},  sub_id: {sub_id}')
        output = ActionResult(code=ExecuteResultEnum.SUCCESS)
        EnvName.IAM_USERNAME = "job_protectEnv_auth_authKey"
        EnvName.IAM_PASSWORD = "job_protectEnv_auth_authPwd"
        param_inst = ResourceParam(req_id)
        param = param_inst.get_param()
        backup_inst = BackUp(req_id, job_id, sub_id, json, param)
        result = backup_inst.backup_task()
        if not result:
            output.code = ExecuteResultEnum.INTERNAL_ERROR
        output_result_file(req_id, output.dict(by_alias=True))
        return ExecuteResultEnum.SUCCESS

    @staticmethod
    @exter_attack
    def allow_backup_in_local_node(req_id, job_id, sub_id, json):
        return GaussDBBackupService.allow_backup_in_local_node(req_id, job_id, sub_id)

    @staticmethod
    @exter_attack
    def query_backup_copy(req_id, job_id, sub_id, json):
        log.info(
            f'start query_backup_copy, req_id: {req_id}, job_id: {job_id},  sub_id: {sub_id}')
        return GaussDBBackupService.query_backup_copy(req_id, job_id, sub_id)

    @staticmethod
    @exter_attack
    def allow_backup_sub_job_in_local_node(req_id, job_id, sub_id, json):
        log.info(
            f'start allow_backup_sub_job_in_local_node, req_id: {req_id}, job_id: {job_id},  sub_id: {sub_id}')
        return GaussDBBackupService.allow_backup_in_local_node(req_id, job_id, sub_id)

    @staticmethod
    @exter_attack
    def check_backup_job_type(req_id, job_id, sub_id, json):
        log.info(
            f'start check_backup_job_type, req_id: {req_id}, job_id: {job_id},  sub_id: {sub_id}')
        return GaussDBBackupService.check_backup_job_type(req_id, job_id, sub_id, json)

    @staticmethod
    @exter_attack
    def backup_prerequisite(req_id, job_id, sub_id, json):
        log.info(
            f'start backup_prerequisite, req_id: {req_id}, job_id: {job_id},  sub_id: {sub_id}')
        return GaussDBBackupService.backup_pre_job(req_id, job_id, sub_id, json)

    @staticmethod
    @exter_attack
    def backup_prerequisite_progress(req_id, job_id, sub_id, json):
        log.info(
            f'start backup_prerequisite_progress, req_id: {req_id}, job_id: {job_id},  sub_id: {sub_id}')
        return GaussDBBackupService.backup_prerequisite_progress(req_id, job_id, sub_id, json)

    @staticmethod
    @exter_attack
    def backup_gen_sub_job(req_id, job_id, sub_id, json):
        log.info(
            f'start backup_gen_sub_job, req_id: {req_id}, job_id: {job_id},  sub_id: {sub_id}')
        return GaussDBBackupService.backup_gen_sub_job(req_id, job_id, sub_id, json)

    @staticmethod
    @exter_attack
    def backup_post_job(req_id, job_id, sub_id, json):
        log.info(
            f'start execute backup_post_job, req_id: {req_id}, job_id: {job_id},  sub_id: {sub_id}')
        GaussDBBackupService.do_post_job(req_id, job_id, sub_id, json)
        output = ActionResult(code=ExecuteResultEnum.SUCCESS)
        output_result_file(req_id, output.dict(by_alias=True))
        return ExecuteResultEnum.SUCCESS

    @staticmethod
    @exter_attack
    def backup_post_job_progress(req_id, job_id, sub_id, json):
        log.info(
            f'start execute backup_post_job_progress, req_id: {req_id}, job_id: {job_id},  sub_id: {sub_id}')
        param_inst = ResourceParam(req_id)
        param = param_inst.get_param()
        backup_inst = BackUp(req_id, job_id, sub_id, json, param)
        backup_inst.backup_post_job_progress()
        return ExecuteResultEnum.SUCCESS
